package junitTests;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

public class LoginTestCase extends SeleneseTestCase {
	@Before
	public void setUp() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://localhost:8888/");
		selenium.start();
	}

	@Test
	public void testSuccessfulLogin() throws Exception {
		selenium.open("/login.jsp");
		assertEquals("Login | Media Vault", selenium.getTitle());
		selenium.type("id=username", "test@gmail.com");
		selenium.type("id=password", "test");
		selenium.click("id=login");
		selenium.waitForCondition("selenium.browserbot.getCurrentWindow().document.getElementById('loginMsgPanel')", "10000");
		assertTrue(selenium.isTextPresent("Your have succesfully logged into Media Vault."));
		selenium.click("link=Continue");
		selenium.waitForPageToLoad("30000");
		assertEquals("Home | Media Vault", selenium.getTitle());
	}
	
	@Test
	public void testIncorrectPassword() throws Exception {
		selenium.open("/login.jsp");
		assertEquals("Login | Media Vault", selenium.getTitle());
		selenium.type("id=username", "test@gmail.com");
		selenium.type("id=password", "incorrect");
		selenium.click("id=login");
		selenium.waitForCondition("selenium.browserbot.getCurrentWindow().document.getElementById('loginMsgPanel')", "10000");
		assertTrue(selenium.isTextPresent("LOGIN ERROR - INCORRECT LOGIN DETAILS"));
		selenium.click("link=Continue");
		selenium.waitForPageToLoad("30000");
	}
	
	@Test (expected = NullPointerException.class)
	public void testIncorrectUsername() throws Exception {
		selenium.open("/login.jsp");
		assertEquals("Login | Media Vault", selenium.getTitle());
		selenium.type("id=username", "incorrect");
		selenium.type("id=password", "test");
		selenium.click("id=login");
	}
	
	@Test (expected = NullPointerException.class)
	public void testIncorrectUsernameAndPassword() throws Exception {
		selenium.open("/login.jsp");
		assertEquals("Login | Media Vault", selenium.getTitle());
		selenium.type("id=username", "incorrect");
		selenium.type("id=password", "incorrect");
		selenium.click("id=login");
	}
	
	@Test (expected = NullPointerException.class)
	public void testNullLogin() throws Exception {
		selenium.open("/login.jsp");
		assertEquals("Login | Media Vault", selenium.getTitle());
		selenium.click("id=login");
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
	
	
}
